using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Collections;
using System.IO;
using System.Configuration;
using codegen.utility.Profile;
using System.Collections.Specialized;

namespace codegen
{
    class Program
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args">
        /// 1. codegen cmd
        /// 2. codegen input file
        /// 3. codegen output dir
        /// 4. codegen arg file
        /// 
        /// </param>
        static void Main(string[] args)
        {
       
            //Object Generate
            Console.WriteLine("Object Generating");
            NameValueCollection section = (NameValueCollection)ConfigurationSettings.GetConfig("db/table");
            string className = section["name"];
            section =(NameValueCollection)ConfigurationSettings.GetConfig("db/field");
            List<string> l = new List<string>();
            for(int i=0;i<section.Keys.Count;i++)
            {
                l.Add(section.GetKey(i));
            }

            template.TObj.CodeGenerate(className,l.ToArray());
            Console.WriteLine("Object Generated");


            //Function Generate
            Console.WriteLine("Function Generating");
            section = (NameValueCollection)ConfigurationSettings.GetConfig("db/function");
            template.TObjFunction.GetObjectByID(section["className"],section["classFileName"]
                ,section["GetByIDInputName"],section["GetByIDOutputList"].Split(','));
            //SetObject
            template.TObjFunction.SetObject(section["className"], section["classFileName"]
                , section["SetInputList"].Split(','), section["SetOutputList"].Split(','));
            
            Console.WriteLine("Function Generated");


            //Svc Generate
            Console.WriteLine("Svc Generating");
            section = (NameValueCollection)ConfigurationSettings.GetConfig("svc/soap");
            template.TSvc.GenerateSOAP(section["SOAPFileName"], section["SOAPName"], section["SOAPField"].Split(','), section["SOAPFieldType"].Split(','));

            template.TSvc.GenerateGetRegister(section["SOAPFileName"], section["SOAPName"]);
            template.TSvc.GenerateSetRegister(section["SOAPFileName"], section["SOAPName"]);
            Console.WriteLine("Svc Generated");

            if (!ValidateArgs(args))
                Console.WriteLine("failed!" + Environment.NewLine);
            return;
        }

        static bool ValidateArgs(string[] args)
        {
            return true;
            if (args.Length == 1)
            {
                if (args[0] == "all")
                { 
                                    
                }
            }
            else if (args.Length == 2)
            { 
            
            }
            else if (args.Length == 3)
            { 
            
            }
            else if (args.Length == 4)
            {

            }
            else
            {
                return false;
            }

        }

        static void f0()
        {

            NameValueCollection section = (NameValueCollection)ConfigurationSettings.GetConfig("db/field");
            string value = section["ID"];
            int cc = section.Keys.Count;

            //ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            //fileMap.ExeConfigFilename = @"codegen.exe.config";
            //// relative path names possible

            //// Open another config file
            //System.Configuration config =
            //   ConfigurationManager.OpenMappedExeConfiguration(fileMap,
            //   ConfigurationUserLevel.None);

            ////read/write from it as usual
            //ConfigurationSection mySection = config.GetSection("mySection");
            //config.SectionGroups.Clear(); // make changes to it

            //config.Save(ConfigurationSaveMode.Full);  // Save changes
        
        
        }
        static void f1()
        {
            objectlib.jeegoonconfig jc = new codegen.objectlib.jeegoonconfig();

            jc.Table = "member";
            List<objectlib.config> cons = new List<codegen.objectlib.config>();
            cons.Add(new codegen.objectlib.config("Name", "string"));
            cons.Add(new codegen.objectlib.config("ID", "int"));
            cons.Add(new codegen.objectlib.config("Name", "string"));
            cons.Add(new codegen.objectlib.config("Email", "string"));
            cons.Add(new codegen.objectlib.config("CreatedDate", "date"));
            cons.Add(new codegen.objectlib.config("ModifiedDate", "date"));
            jc.Fields = cons.ToArray();

            XmlSerializer xs = new XmlSerializer(typeof(objectlib.jeegoonconfig));
            {
                if (System.IO.File.Exists("class.name" + ".jeegoon"))
                {
                    System.IO.File.Delete("class.name" + ".jeegoon");
                }
                FileStream fs;
                if ("class.name".ToLower().EndsWith(".jeegoon"))
                {
                    fs = new FileStream("class.name", FileMode.CreateNew);
                }
                else
                {
                    fs = new FileStream("class.name" + ".jeegoon", FileMode.CreateNew);
                }
                xs.Serialize(fs, jc);
                fs.Close();
            }
        
        }



        static void f2()
        {
            string[][] jc = new string[3][];
            jc[0] = new string[] { "member" };
            jc[1] = new string[] { "sss", "cccc" };
            jc[2] = new string[] { "111sss", "cccc", "fsafs" };

            XmlSerializer xs = new XmlSerializer(typeof(string[][]));
            {
                if (System.IO.File.Exists("class.name" + ".jeegoon"))
                {
                    System.IO.File.Delete("class.name" + ".jeegoon");
                }
                FileStream fs;
                if ("class.name".ToLower().EndsWith(".jeegoon"))
                {
                    fs = new FileStream("class.name", FileMode.CreateNew);
                }
                else
                {
                    fs = new FileStream("class.name" + ".jeegoon", FileMode.CreateNew);
                }
                xs.Serialize(fs, jc);
                fs.Close();
            }

        }
    }
}